<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${class}">
    <sql id="qrySelect">
        ${fieldAndSrcNames}
    </sql>

    <select id="selectById" resultType="${class}">
        <include refid="qrySelect"/>
        from ${tableName}
        where
        ${key}
    </select>

    <insert id="insert">
        <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
            SELECT LAST_INSERT_ID() AS id
        </selectKey>
        insert into ${tableName} (
        ${fieldNames}
            ) values (
        ${srcNames}
        )
    </insert>

    <update id="updateById">
        update ${tableName}
        <set>
        <#list properties as prop>
            <if test="${prop.name?uncap_first} != null">
                ${prop.srcName}=${r"#{"}${prop.name?uncap_first}${r"}"}
            </if>
        </#list>
        </set>
        where ${key}
    </update>

    <delete id="deleteById">
        delete from ${tableName} where ${key}
    </delete>


    <sql id="dynamicWhere">
        <trim prefix="where" prefixOverrides="and | or">
        <#list properties as prop>
            <if test="${prop.name?uncap_first} != null">
                ${prop.srcName}=${r"#{"}${prop.name?uncap_first}${r"}"}
            </if>
        </#list>
        </trim>
    </sql>

    <sql id="dynamicOrderBy">
        <trim prefix=" order by ">
            ${r"${"}orderBy${r"}"}
        </trim>
    </sql>

    <select id="selectByCond" resultType="${class}">
        <include refid="qrySelect"/>
        from ${tableName}
        <include refid="dynamicWhere"/>
        <include refid="dynamicOrderBy"/>
    </select>

    <select id="selectCountByCond" resultType="Integer">
        select count(1) as kk
        from ${tableName}
        <include refid="dynamicWhere"/>
    </select>

</mapper>
